#include "offlinemessagemodel.hpp"
#include "db.h"

  //存储用户离线信息
bool OfflineMsgModel::insert(int userid, string msg)
{
  //1.组装sql语句
  char sql[1024] = {0};
  sprintf(sql,"insert into OfflineMessage(userid, message) values('%d', '%s');",userid, msg.c_str());

    MySQL mysql;
    if(mysql.connect()) {
      if(mysql.update(sql)) {

        return true;
      }
    }
  return false;
}

  //删除用户的离线消息
void OfflineMsgModel::remove(int userid)
{
  //1.组装sql语句
  char sql[1024] = {0};
  sprintf(sql,"delete from OfflineMessage where userid = %d;",userid);

    MySQL mysql;
    if(mysql.connect()) {
      if(mysql.update(sql)) {

      }
    }

}

  // 查询用户的离线消息
vector<string> OfflineMsgModel::query(int userid)
{
  //1.组装sql语句
  char sql[1024] = {0};
  sprintf(sql,"select message from OfflineMessage where userid = %d;",userid);

  vector<string> vec;
    MySQL mysql;
    if(mysql.connect()) {
      MYSQL_RES *res = mysql.query(sql);
      if(res != NULL) {
        //将userid用户的所有离线消息放入vec中返回
        MYSQL_ROW row ;
        while((row = mysql_fetch_row(res)) != nullptr)
        {
          vec.push_back(row[0]);
        }
        mysql_free_result(res);
        return vec;       
      }
    }
  return vec;
}